<template>
	<view class="page">
		<view class="row">
		</view>
		<view class="row" v-show="showListView">
			<button class="button" size="mini" @click="startScan()">开始扫描</button>
		</view>

		<view class="row">
			<text>日志</text>
		</view>

		<view class="row" v-show="showListView">
			<text>设备列表</text>
			<text>({{deviceList.length}})</text>
		</view>
		<scroll-view class="border list" scroll-y="true" :style="{width:'93vw',height:'50vh'}" v-show="showListView">
			<view v-for="(item,index) in deviceList" :key="index" class="list-item" @click="toConnect(item.identifier)">
				<view class="row" js>
					<view class="column">
						<text class="text">{{item.name}}</text>
						<text class="text">{{item.macAddress}}</text>
					</view>
					<text class="text">{{item.rssiValue}}</text>
				</view>
			</view>
		</scroll-view>
		<scroll-view class="border list" scroll-y="true" :style="{width:'93vw',height:'50vh'}" v-show="!showListView">
			<view v-for="(item,index) in deviceInfoList" :key="index" class="list-item"
				@click="deviceInfoItemClick(index)">
				<text class="text">{{item.text}}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	var systemInfo;
	var ble;
	var _this;
	var identifier = "";
	export default {
		data() {
			return {
				title: 'Plugin example app',
				deviceList: [],
				deviceInfoList: [{
						text: "1.DisConnect Device"
					},
					{
						text: "2.Health data"
					},
					{
						text: "3.Query"
					},
					{
						text: "4.Setting"
					},
					{
						text: "5.App Controller"
					},
					{
						text: "6.Device Controller"
					},
					{
						text: "7.ECG"
					},
					{
						text: "8.OtaDeviceWidget"
					},
					{
						text: "9.Collection data"
					},
					{
						text: "10.Watch face"
					},
					{
						text: "11.Log"
					}
				],
				showListView: true
			};
		},
		onLoad() {
			_this = this;
			_this.showListView = true;
			_this.initMethods()
		},

		methods: {
			//判断是安卓还是ios系统
			isAndroidOrIOS() {
				let type = uni.getSystemInfoSync().platform
				switch (type) {
					case 'android':
						systemInfo = "android";
						break;
					case 'ios':
						systemInfo = "ios";
						break;
					default:
						systemInfo = "other";
						break;
				}
			},

			initMethods() {
				_this.isAndroidOrIOS();
				if (systemInfo == "android") {
					//安卓系统加载插件
					console.log("android bluetooth-ycSdkPlugin");
					ble = uni.requireNativePlugin('bluetooth-ycSdkPlugin');
				} else {
					//ios系统加载插件
					console.log("ios DCTestUniPlugin");
					ble = uni.requireNativePlugin('DCTestUniPlugin');
				}
				console.log("initPlugin start123" + ble);
				//初始化插件
				ble.initPlugin((res) => {
					console.log("initPlugin" + res.message);

				});
				console.log("initPlugin end")
			},
			//点击item启动扫描
			startScan() {
				console.log('startScan2002' + ble);
				uni.showLoading({
					title: '正在扫描111111111111111111111111111111...',
					mask: false
				});

				if (systemInfo == "android") {
					console.log('android scanDevice');
					//申请安卓权限
					plus.android.requestPermissions(["android.permission.ACCESS_FINE_LOCATION",
						"android.permission.ACCESS_FINE_LOCATION"
					], function(e) {
						console.log('uniapp有权限' + JSON.stringify(e));
					});

					plus.android.requestPermissions(["android.permission.BLUETOOTH", "android.permission.BLUETOOTH_SCAN",
						"android.permission.BLUETOOTH_CONNECT"
					], function(e) {
						console.log('uniapp有权限' + JSON.stringify(e));
						uni.showLoading({
							title: '加载中'
						});
						//有权限再扫描设备
						ble.scanDevice(5, (res) => {
							uni.hideLoading();
							console.log(res.data);
							let list = Array.from(res.data);

							_this.deviceList = [];
							//遍历设备添加到列表
							list.forEach(item => {
								console.log("设备信息name:" + item.name + "--macAddress:" + item
									.macAddress);
								item.identifier = item.macAddress;
								if (item.macAddress != "" && item.rssiValue != "0") {
									_this.deviceList.push(item);
								}

							});
						});
					});
				} else {
					console.log('ios scanDevice111:' + JSON.stringify(ble));
					//ios扫描设备
					ble.scanDevice(6, (res) => {
						uni.hideLoading();
						console.log(res.data);
						let list = Array.from(res.data);

						list.sort((a, b) => parseInt(b.rssiValue) - parseInt(a.rssiValue));
						_this.deviceList = [];
						list.forEach(item => {
							console.log("设备信息name:" + item.name + "--macAddress:" + item
								.macAddress + "--identifier:" + item
								.identifier);
							if (item.macAddress != "" && item.rssiValue != "0") {
								_this.deviceList.push(item);
							}

						});
					});
				}
			},
			//点击item连接设备
			toConnect(address) {
				identifier = address;
				uni.showLoading({
					title: '加载中'
				});
				console.log("连接设备" +
					address);
				ble.connectDevice(address, (res) => {
					uni.hideLoading();
					console.log("连接设备" +
						res.data);
						//连接成功查询健康数据
					this.showListView = false;
					ble.queryDeviceHealthData(0, (res) => {
						uni.hideLoading();
						console.log("步数,距离，卡路里" + JSON.stringify(res.data));
						_this.message = JSON.stringify(res.data);
					});

					ble.queryDeviceHealthData(2, (res) => {
						uni.hideLoading();
						console.log("心率" + JSON.stringify(res.data));
						_this.message = JSON.stringify(res.data);
					});
					ble.queryDeviceHealthData(3, (res) => {
						uni.hideLoading();
						console.log("血压" + JSON.stringify(res.data));
						_this.message = JSON.stringify(res.data);
					});
					ble.queryDeviceHealthData(4, (res) => {
						uni.hideLoading();
						console.log("组合数据" + JSON.stringify(res.data));
						_this.message = JSON.stringify(res.data);
					});
					ble.queryDeviceHealthData(6, (res) => {
						uni.hideLoading();
						console.log("运动数据" + JSON.stringify(res.data));
						_this.message = JSON.stringify(res.data);
					});
					ble.queryDeviceHealthData(7, (res) => {
						uni.hideLoading();
						console.log("bodyIndexData" + JSON.stringify(res.data));
						_this.message = JSON.stringify(res.data);
					});

				});
			},

			deviceInfoItemClick(index) {
				console.log('deviceInfoItemClick.index:' + index);
				switch (index) {
					case 0:
						// uni.showLoading({
						// 	title: '加载中'
						// });
						// uni.showToast({
						// 	title: '断连成功',
						// 	duration: 2000
						// });
						//  _this.showListView = true;
						if (systemInfo == "android") {
							ble.disconnectDevice((res) => {
								uni.hideLoading();
								uni.showToast({
									title: '断连成功',
									duration: 2000
								});
								_this.showListView = true;
							});
						} else {
							ble.disConnectDevice(identifier, (res) => {
								uni.hideLoading();
								uni.showToast({
									title: '断连成功',
									duration: 2000
								});
								_this.showListView = true;
							});
						}
						// var adapter=plus.android.importClass("android.bluetooth.BluetoothAdapter");
						// var adapter2=adapter.getDefaultAdapter();
						// if(adapter2.isEnabled()){
						// 		console.log('关闭蓝牙');
						// adapter2.disable();	
						// }
						break;
					case 1:
						uni.navigateTo({
							url: "health_data/health_data"
						});
						break;
					case 2:
						uni.navigateTo({
							url: "query/query"
						});
						break;
					case 3:
						uni.navigateTo({
							url: "setting/setting"
						});
						break;
					case 4:
						uni.navigateTo({
							url: "appController/appController"
						});
						break;
					case 5:
						uni.navigateTo({
							url: "deviceController/deviceController"
						});
						break;
					case 6:
						uni.navigateTo({
							url: "ecg/ecg"
						});
						break;
					case 7:
						uni.navigateTo({
							url: "ota/ota"
						});
						break;
					case 8:
						uni.navigateTo({
							url: "collection_data/collection_data"
						});
						break;
					case 9:
						uni.navigateTo({
							url: "watch_face/watch_face"
						});
						break;
					case 10:
						uni.navigateTo({
							url: "log/log"
						});
						break;
				}
			},
		}
	}
</script>

<style lang="scss">
	.page {
		width: 100vw;
	}

	.row {
		display: flex;
		flex-direction: row;
		align-content: flex-start;
		align-items: flex-start;
		margin: 15px 10px;
		padding: 5px;

		.button {
			width: 100px;
			font-size: 11px;
			background-color: #003366;
			color: white;
		}
	}

	.border {
		border: solid 1px gray;
	}

	.list {
		padding-left: 3vw;
		padding-right: 3vw;
		padding-top: 3vw;

		.list-item {
			background-color: #f8f8f8;
			border: solid 1px darkgray;
			box-shadow: 1px 1px 3px;
			margin-bottom: 3vw;

			.text {
				height: 30px;
				line-height: 30px;
				margin-left: 5px;
				color: #003366;
			}

			.row {
				margin: 3px 10px !important;
				padding: 0 !important;
			}
		}
	}

	.log {
		width: 87vw;
		font-size: 13px;
		color: #003366;

		.log-bottom {
			height: 10vw;
		}
	}

	// .loading {
	// 	/* 样式按需自定义 */
	// 	position: fixed;
	// 	top: 50%;
	// 	left: 50%;
	// 	transform: translate(-50%, -50%);
	// }
</style>